package com.example.calender.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.calender.BaseActivity;
import com.example.calender.R;
import com.example.calender.entity.Event;
import com.example.calender.utils.CalendarUtils;
import com.example.calender.utils.ToastUtil;

import java.util.Date;

import static com.example.calender.utils.DateUtlis.strDate;

public class EventActivity extends BaseActivity {

    ImageButton ibback;
    TextView tvtitle, tvtime, tvupdata;
    EditText title, localtion, details;
    private String isAdd;
    private Date date;

    public static void start(Context context,String isAdd){
        Intent intent = new Intent(context, EventActivity.class);
        intent.putExtra("isAdd",isAdd);
        context.startActivity(intent);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_event;
    }

    @Override
    protected void findViews() {
        ibback = findViewById(R.id.ibback);
        tvtitle = findViewById(R.id.tvtitle);
        title = findViewById(R.id.title);
        tvtime = findViewById(R.id.tv_time);
        localtion = findViewById(R.id.localtion);
        details = findViewById(R.id.details);
        tvupdata = findViewById(R.id.tv_updata);
    }

    @Override
    protected void initView() {
        tvtime.setText(strDate(new Date()));
    }

    @Override
    protected void initListener() {
        ibback.setOnClickListener(v -> finish());
        tvupdata.setOnClickListener(v -> {
            String title = EventActivity.this.title.getText().toString().trim();
            String localtion = EventActivity.this.localtion.getText().toString().trim();
            String details = EventActivity.this.details.getText().toString().trim();
            if (isAdd!=null){
                CalendarUtils.deleteCalendarEvent(EventActivity.this,isAdd);
            }else {
                if (TextUtils.isEmpty(title)) {
                    ToastUtil.showShort("标题不能为空");
                    return;
                }
                CalendarUtils.addCalendarEvent(EventActivity.this,
                        title,
                        details,
                        date.getTime(),
                        localtion,
                        0);
            }
        });
        tvtime.setOnClickListener(v->{
            calendarView();
        });
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        isAdd = intent.getStringExtra("isAdd");
        if (isAdd!=null){
            tvtitle.setText("查看事件");
            tvupdata.setText("删除");
            Event allEvent2 = CalendarUtils.findAllEvent2(isAdd);
            if (allEvent2!=null){
                tvtime.setText(strDate(new Date(allEvent2.getDtstart())));
                tvtime.setEnabled(false);
                title.setText("事件标题："+allEvent2.getEventTitle());
                title.setEnabled(false);
                localtion.setText("地点："+allEvent2.getLocation());
                localtion.setEnabled(false);
                details.setText("备注："+allEvent2.getDescription());
                details.setEnabled(false);
            }
        }else {
            tvtitle.setText("创建事件");
            tvupdata.setText("保存");
        }
    }

    /**
     * 时间选择
     */
    private void calendarView() {
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(EventActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                EventActivity.this.date = date;
                tvtime.setText(strDate(date));
            }
        }).setDividerColor(Color.argb(255,3,169,244))
                .setTitleText("选择事件时间")
                .setLineSpacingMultiplier(3f)
                .setType(new boolean[]{true, true, true, true, true, false})
                .build();
        pvTime.show();
    }
}